﻿using MeetingRoomBooking.Application.Common.Interfaces;
using MeetingRoomBooking.Application.Common.Mappings;
using MeetingRoomBooking.Application.Common.Models;

namespace MeetingRoomBooking.Application.Rooms.Queries;
public record GetUsersWithPaginationQuery : IRequest<PaginatedList<UserViewModal>>
{
    public int PageNumber { get; init; } = 1;
    public int PageSize { get; init; } = 10;
}

public class GetUsersWithPaginationQueryHander : IRequestHandler<GetUsersWithPaginationQuery, PaginatedList<UserViewModal>>
{
    private readonly IApplicationDbContext _context;

    public GetUsersWithPaginationQueryHander(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<PaginatedList<UserViewModal>> Handle(GetUsersWithPaginationQuery request, CancellationToken cancellationToken)
    {
        var users = await _context.Users
           .OrderBy(x => x.Id)
           .Select(x => new UserViewModal
           {
               UserName = x.UserName,
               Id = x.Id,
               Role = x.Role,
               Email = x.Email,
               Created = x.Created.ToString("yyyy-MM-dd"),
               CreatedBy = x.CreatedBy,
               LastModified = x.LastModified.ToString("yyyy-MM-dd"),
               LastModifiedBy = x.LastModifiedBy
           })
           .PaginatedListAsync(request.PageNumber, request.PageSize);

        return users;
    }
}
